package windows;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.Vector;
import java.awt.Font;

import main.ImageLibrary;
import main.Telecommando;

import base.*;

public class Password extends Window {

	private Vector<Button> buttonsNum = new Vector<Button>( );

	// Window and button variables
	private Button pressed;
	private boolean butPressed = false;
	private int channel = 0;

	private int curBuildChannel = 0;

	public Password( )
	{
		// Rooms:  Bedroom, Living Room, Kitchen
		super( "Password" );
		// -=-=-=-=-=-=-=-=-=- Model 1 Of Number Pad -=-=-=-=-=-=-=-=-=-

		Point numPadStart = new Point( 6, 62);
		
		ImageLibrary.add( "number_border", "Images/TVRemote/Model_2/number_border.png" );
		
		// Set up points for number pad
		Point onePos   = new Point( numPadStart.x + 0,   numPadStart.y + 0);
		Point twoPos   = new Point( numPadStart.x + 51,  numPadStart.y + 0);
		Point threePos = new Point( numPadStart.x + 102, numPadStart.y + 0);
		Point fourPos  = new Point( numPadStart.x + 0,   numPadStart.y + 51);
		Point fivePos  = new Point( numPadStart.x + 51,  numPadStart.y + 51);
		Point sixPos   = new Point( numPadStart.x + 102, numPadStart.y + 51);
		Point sevenPos = new Point( numPadStart.x + 0,   numPadStart.y + 102);
		Point eightPos = new Point( numPadStart.x + 51,  numPadStart.y + 102);
		Point ninePos  = new Point( numPadStart.x + 102, numPadStart.y + 102);
		Point backPos  = new Point( numPadStart.x + 0,   numPadStart.y + 153);
		Point zeroPos  = new Point( numPadStart.x + 51,  numPadStart.y + 153);
		Point enterPos = new Point( numPadStart.x + 102, numPadStart.y + 153);
		
		buttonsNum.add( new Button( "one_button",   onePos,   "Images/TVRemote/Model_2/button_1.png",         false, true ) );
		buttonsNum.add( new Button( "two_button",   twoPos,   "Images/TVRemote/Model_2/button_2.png",         false, true ) );
		buttonsNum.add( new Button( "three_button", threePos, "Images/TVRemote/Model_2/button_3.png",         false, true ) );
		buttonsNum.add( new Button( "four_button",  fourPos,  "Images/TVRemote/Model_2/button_4.png",         false, true ) );
		buttonsNum.add( new Button( "five_button",  fivePos,  "Images/TVRemote/Model_2/button_5.png",         false, true ) );
		buttonsNum.add( new Button( "six_button",   sixPos,   "Images/TVRemote/Model_2/button_6.png",         false, true ) );
		buttonsNum.add( new Button( "seven_button", sevenPos, "Images/TVRemote/Model_2/button_7.png",         false, true ) );
		buttonsNum.add( new Button( "eight_button", eightPos, "Images/TVRemote/Model_2/button_8.png",         false, true ) );
		buttonsNum.add( new Button( "nine_button",  ninePos,  "Images/TVRemote/Model_2/button_9.png",         false, true ) );
		buttonsNum.add( new Button( "back_button",  backPos,  "Images/TVRemote/Model_2/button_num_back.png",  false, true ) );
		buttonsNum.add( new Button( "zero_button",  zeroPos,  "Images/TVRemote/Model_2/button_0.png",         false, true ) );
		buttonsNum.add( new Button( "enter_button", enterPos, "Images/TVRemote/Model_2/button_num_enter.png", false, true ) );
	}
	
	@Override
	public void draw(Graphics2D g2d) {
		for ( Button but : buttonsNum )
				but.draw( g2d );

		// Writing Number
		int fontSize = 42;
		Point labelStart  = new Point( 12, 48 );
		Point borderStart = new Point( 6, 6 );
		g2d.setColor(Color.WHITE);
		Font numFont  = new Font("NumFont", Font.BOLD, fontSize );
		g2d.setFont(numFont);
		String text = "" + curBuildChannel;
		g2d.drawString( text, labelStart.x, labelStart.y );
		g2d.drawImage( ImageLibrary.get("number_border"), borderStart.x, borderStart.y, Telecommando.getInstance( ) );
	}

	@Override
	public void fingerPressed(Point location) {

		for( Button but : buttonsNum ) {
			if( but.press( location.x , location.y ) ) {
				pressed = but;
				butPressed = true;
			}
		}
	}

	@Override
	public void fingerReleased(Point location) {
		
		boolean skipAction = false;
		
		// this if section will tell this function to not perform button actions if a button
		// was pressed but released off of that button
		if ( butPressed ) {
			if ( !pressed.getRect( ).contains(location) ) { 
				//The button released in is outside of pressed button; don't do button call
				skipAction = true;
				pressed.release();
			}
		}
		
		if ( !skipAction) {
			for( Button but : buttonsNum ) {
				if( but.isPressed( ) ) {
					
					String butName = but.getName( );
					int pushedNumber = 0;
					boolean skipBuildNewChannel = false;
					
					if ( butName.equals("one_button") ) 
						pushedNumber = 1;
					else if ( butName.equals("two_button") ) 
						pushedNumber = 2;
					else if ( butName.equals("three_button") ) 
						pushedNumber = 3;
					else if ( butName.equals("four_button") ) 
						pushedNumber = 4;
					else if ( butName.equals("five_button") ) 
						pushedNumber = 5;
					else if ( butName.equals("six_button") ) 
						pushedNumber = 6;
					else if ( butName.equals("seven_button") ) 
						pushedNumber = 7;
					else if ( butName.equals("eight_button") ) 
						pushedNumber = 8;
					else if ( butName.equals("nine_button") ) 
						pushedNumber = 9;
					else if ( butName.equals("zero_button") ) 
						pushedNumber = 0;
					if ( !skipBuildNewChannel ) {
						int newBuildChannel = 10 * curBuildChannel + pushedNumber;
						curBuildChannel = newBuildChannel;

						if ( curBuildChannel >= 1000 && curBuildChannel < 10000 ) {
							channel = curBuildChannel;
							System.out.println("Password set to " + channel );
							if( channel == Telecommando.PASSWORD )
								Telecommando.viewPassword = false;
							curBuildChannel = 0;
						}
					}
					but.release( );
				}
			}
		}

		// Because all buttons are released, no button could be pressed right now
		pressed = null;
		butPressed = false;
	}

	@Override
	public void gestureHorizontal(int amount) {
		
	}

	@Override
	public void gestureVertical(int amount) {
		
	}

}
